<?php

/**
 * This is the model class for table "{{user}}".
 *
 * The followings are the available columns in table '{{user}}':
 * @property string $userId
 * @property string $userName
 * @property string $roles
 * @property integer $version
 * @property string $tagline
 * @property string $password
 * @property string $avatar
 * @property string $jobTitle
 * @property string $email
 * @property string $emailPreference
 * @property string $website
 * @property integer $active
 * @property string $lastLogin
 * @property string $lastIp
 * @property string $uuid
 * @property string $facebook
 * @property string $twitter
 * @property string $openid
 * @property string $createdBy
 * @property string $modifiedBy
 * @property string $modifiedOn
 * @property string $createdOn
 * @property string $companyId
 *
 * The followings are the available model relations:
 * @property Company $company
 */
class User extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userName, password', 'required'),
			array('version, active', 'numerical', 'integerOnly'=>true),
			array('userName, tagline, jobTitle, email, emailPreference, website', 'length', 'max'=>100),
			array('roles, avatar', 'length', 'max'=>200),
			array('password', 'length', 'max'=>40),
			array('lastIp', 'length', 'max'=>20),
			array('uuid, facebook, twitter, openid', 'length', 'max'=>50),
			array('createdBy, modifiedBy, companyId', 'length', 'max'=>10),
			array('lastLogin, modifiedOn, createdOn', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
            
            array('uuid','default','value'=>YII::app()->user->uuid),
            array('facebook','default','value'=>YII::app()->user->facebook),
            array('twitter','default','value'=>YII::app()->user->twitter),
            
			array('userId, userName, roles, version, tagline, password, avatar, jobTitle, email, emailPreference, website, active, lastLogin, lastIp, uuid, facebook, twitter, openid, createdBy, modifiedBy, modifiedOn, createdOn, companyId', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			//'company' => array(self::BELONGS_TO, 'Company', 'companyId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'userId' => 'User',
			'userName' => '用户名',
			'roles' => '角色',
			'version' => '版本',
			'tagline' => '是否在线',
			'password' => '密码',
			'avatar' => 'Avatar',
			'jobTitle' => 'Job Title',
			'email' => 'Email',
			'emailPreference' => 'Email',
			'website' => 'Website',
			'active' => 'Active',
			'lastLogin' => '上次登录时间',
			'lastIp' => '上次登录IP',
			'uuid' => '区',
			'facebook' => '街道',
			'twitter' => '社区',
			'openid' => 'openid',
			'createdBy' => '区',
			'modifiedBy' => '街道',
			'modifiedOn' => '修改时间',
			'createdOn' => '创建时间',
			'companyId' => '社区',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('userId',$this->userId,true);
		$criteria->compare('userName',$this->userName,true);
		$criteria->compare('roles',$this->roles,true);
		$criteria->compare('version',$this->version);
		$criteria->compare('tagline',$this->tagline,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('jobTitle',$this->jobTitle,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('emailPreference',$this->emailPreference,true);
		$criteria->compare('website',$this->website,true);
		$criteria->compare('active',$this->active);
		$criteria->compare('lastLogin',$this->lastLogin,true);
		$criteria->compare('lastIp',$this->lastIp,true);
        $criteria->addSearchCondition('uuid',YII::app()->user->uuid);
        $criteria->addSearchCondition('facebook',YII::app()->user->facebook);
        $criteria->addSearchCondition('twitter',YII::app()->user->twitter);


		//$criteria->compare('uuid',$this->uuid,true);
		//$criteria->compare('facebook',$this->facebook,true);
		//$criteria->compare('twitter',$this->twitter,true);
		$criteria->compare('openid',$this->openid,true);
		$criteria->compare('createdBy',$this->createdBy,true);
		$criteria->compare('modifiedBy',$this->modifiedBy,true);
		$criteria->compare('modifiedOn',$this->modifiedOn,true);
		$criteria->compare('createdOn',$this->createdOn,true);
		$criteria->compare('companyId',$this->companyId,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
